from test.common.browser import Browser
import utils.generator as gen
from selenium.webdriver.support.select import Select


class Page(Browser):
    def __init__(self, page=None, browser_type='firefox'):
        if page:
            self.driver = page.driver
        else:
            super(Page, self).__init__(browser_type=browser_type)

    def get_driver(self):
        return self.driver

    def open_new_page(self, url):
        """
        打开新的标签页
        :@param url 请求地址
        """
        js = 'window.open("' + url + '");'
        self.driver.execute_script(js)

    def find_element(self, *args):
        return self.driver.find_element(*args)

    def find_elements(self, *args):
        return self.driver.find_elements(*args)

    def find_element_by_id(self, *args):
        return self.driver.find_element_by_id(*args)

    def find_element_by_xpath(self, *args):
        return self.driver.find_element_by_xpath(*args)

    def find_elements_by_xpath(self, *args):
        return self.driver.find_elements_by_xpath(*args)

    def find_element_by_id_yml(self, action):
        return self.driver.find_element_by_id(action.get("id"))

    def find_element_by_xpath_yml(self, action):
        return self.driver.find_element_by_xpath(action.get("path"))

    def find_elements_by_xpath_yml(self, action):
        return self.driver.find_elements_by_xpath(action.get("path"))

    def send_key(self, action):
        """
        赋值
        :param action: yml文件数据集
        :return:
        """
        set_value_method = action.get("set_value_method")
        if set_value_method:
            # 判断是否需要set数字值
            if action.get("set_value_start_num") and action.get("set_value_end_num"):
                self.element.send_keys(
                    self.switch_set_value_yml.get(set_value_method)(action.get("set_value_start_num"),
                                                                    action.get("set_value_end_num")))
            else:
                self.element.send_keys(self.switch_set_value_yml.get(set_value_method)())
        else:
            self.element.send_keys(action.get("default_value"))

    def select_by_value(self, action):
        """
        选择下拉框的值
        :param action: yml文件数据集
        :return:
        """
        set_value_method = action.get("set_value_method")
        if set_value_method:
            # 判断是否需要set数字值
            if action.get("set_value_start_num") and action.get("set_value_end_num"):
                Select(self.element).select_by_value(
                    self.switch_set_value_yml.get(set_value_method)(action.get("set_value_start_num"),
                                                                    action.get("set_value_end_num")))
            else:
                Select(self.element).select_by_value(self.switch_set_value_yml.get(set_value_method)())
        else:
            Select(self.element).select_by_value(action.get("default_value"))

    def element_click(self, action):
        """
        点击操作
        :return:
        """
        self.element.click()

    # 定义find_method字段对应的调用方法的字段switch_method
    switch_method_yml = {"id": find_element_by_id_yml,
                         "xpath": find_element_by_xpath_yml,
                         "xpath_list": find_elements_by_xpath_yml
                         }
    # 定义type字段对应的调用方法的字段switch_type
    switch_type_yml = {"set_by_value": send_key,
                       "select_by_value": select_by_value,
                       "click": element_click
                       }
    # 定义set值方法
    switch_set_value_yml = {"phone_number": gen.random_phone_number,
                            "people_name": gen.random_name,
                            "address": gen.random_address,
                            "email": gen.random_email,
                            "ipv4": gen.random_ipv4,
                            "vehicle_num": gen.random_vehicle_num,
                            "id_card_num": gen.random_id_card,
                            "random_num_str": gen.random_num_str
                            }
